﻿using Jw.Core;
using Jw.Models;
using Jw.Web.Areas.Weight.Common;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;

namespace Jw.Web.Areas.Weight.Controllers
{
    public class MaterialController : Controller
    {
        //
        // GET: /Weight/Material/
        public List<dynamic> GetJinChangItems()
        {
            var result = new List<dynamic>();
            result.Add(new { value = "0", text = "已进厂" });
            result.Add(new { value = "1", text = "已出厂" });
            result.Add(new { value = "2", text = "异常" });
            return result;
        }

        public List<dynamic> GetJinChangFSItems()
        {
            var result = new List<dynamic>();
            result.Add(new { value = "0", text = "物资检斤" });
            result.Add(new { value = "1", text = "销售检斤" });
            result.Add(new { value = "2", text = "内转检斤" });
            return result;
        }
        public ActionResult Index()
        {
            var codeService = new sys_codeService();
            var model = new
            {
                idField = "S_CRCJJBH", //主键
                urls = new
                {
                    query = "/api/Weight/Material/GetMateriaQuery/"
                },
                dataSource = new
                {
                    yearItems = codeService.GetYearItems(),
                    monthItems = codeService.GetMonthItems(),
                    jinchangFSItems = GetJinChangFSItems(),
                    jinchangItems = GetJinChangItems()
                },
                form = new
                {
                    S_CRCJJBH = "",
                    S_CPH = "",
                    D_RCSJ = "",
                    B_BS = "",
                    I_JCFS = "",
                    S_FHDWMC = ""
                }
            };
            return View(model);
        }

    }

    public class MaterialApiController : ApiController
    {

        public dynamic GetMateriaQuery(RequestWrapper query)
        {
            query.LoadSettingXmlString(@"
            <settings defaultOrderBy='S_CRCJJBH DESC'>
            <select>*</select>
                <from>JJ_WLJCRKJJ</from>
                    <where defaultForAll='true' defaultCp='equal' defaultIgnoreEmpty='true' >
                        <field name='S_CRCJJBH' cp='like'></field>
                        <field name='S_CPH' cp='like'></field>
                        <field name='D_RCSJ' cp='daterange'></field>
                    </where>
            </settings>");
            var service = new JJ_WLJCRKJJService();
            var pQuery = query.ToParamQuery();
            var result = service.GetDynamicListWithPaging(pQuery);

            return result;
        }

        public dynamic GetEditMaster(string id)
        {
            JJ_WLJCRKJJ obj = new JJ_WLJCRKJJService().GetModel(ParamQuery.Instance().AndWhere("S_CRCJJBH", id));
            return new
            {
                form = obj,
                scrollKeys = new JJ_WLJCRKJJService().ScrollKeys("Tzrytz_BillNo", id.ToString())
            };
        }

        public void Edit(dynamic data)
        {
            var formWrapper = RequestWrapper.Instance().LoadSettingXmlString(@"
            <settings>
                <table>{0}</table>
                <where><field name='S_CRCJJBH' cp='equal'></field></where>
            </settings>", typeof(JJ_WLJCRKJJ).Name);

            var result = new JJ_WLJCRKJJService().Edit(formWrapper, null, data);
        }


        /// <summary>
        /// 删除主表（删除主表前是删除明细)
        /// </summary>
        /// <param name="id"></param>
        public void Delete(string id)
        {
            var result = new JJ_WLJCRKJJService().Delete(ParamDelete.Instance().AndWhere("S_CRCJJBH", id));
            MaterialHelper.ThrowHttpExceptionWhen(result <= 0, "单据删除失败[Ldphbz_BillNo={0}]，请重试或联系管理员！", id);
        }


    }

}
